<template>
  <el-dialog
    :title="form.title"
    :visible.sync="open"
    width="60%"
    top="10%"
    :append-to-body="true"
    :modal-append-to-body="true"
    :modal="true"
    :close-on-click-modal="false"
  >
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      border
      show-summary
      :summary-method="getSummaries"
      class="inputTable"
    >
      <el-table-column label="序号" align="center" prop="index" width="70">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="materialCode" align="center" label="物资编码" />
      <el-table-column prop="materialName" align="center" label="物资名称" width="300" show-overflow-tooltip />
      <el-table-column prop="otherAttrName" align="center" label="建设子专业" />
      <el-table-column prop="currentAmount" align="center" label="本次绑定数量">
        <template slot-scope="scope">
          <el-input-number
            v-if="form.pageType === 'edit'"
            v-model.trim="scope.row.currentAmount"
            :precision="scope.row.spuUnitAcc"
            :min="0"
            :controls="false"
            @input="currentAmountInput($event, scope, 'currentAmount')"
          />
          <span v-else>{{ scope.row.currentAmount }}</span>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button v-if="form.pageType === 'edit'" class="pms-button-primary" @click="handleSend">保存</el-button>
      <el-button class="pms-button-primary" @click="open = false">关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
// import * as Axios from '@/api/settlement'
import { deepCopy } from '@/utils'
import { inputValidation } from '@/views/settlement/settlementToExamine/common'

export default {
  name: 'ProductBunding',
  data() {
    return {
      open: false,
      form: {},
      loading: false,
      tableData: [],
      beforeTableData: [],
      sumNumber: 0
    }
  },
  methods: {
    handleOpen(val) {
      this.form = val
      this.open = true
      this.getList()
    },
    getList() {
      // this.loading = true
      // const obj = {
      //   bundingId: this.form.bundingId,
      //   materialCode: this.form.materialCode
      // }
      // Axios.queryTMaterialCodeOthersSubSpec(obj).then(res => {
      //   const data = res.data.map(item => {
      //     return {
      //       ...item,
      //       currentAmount: item.currentAmount - 0
      //     }
      //   })
      //   this.beforeTableData = deepCopy(data)
      //   this.tableData = data
      //   this.loading = false
      // })
    },
    getSummaries({ columns, data }) {
      // const sums = []
      // columns.forEach((column, index) => {
      //   if (index === 1) {
      //     sums[index] = data[0] && data[0].materialCode
      //     return
      //   }
      //   if (index === 2) {
      //     sums[index] = data[0] && data[0].materialName
      //     return
      //   }
      //   if (index === 3) {
      //     sums[index] = '可绑定总数：' + this.form.amount
      //     return
      //   }
      //   if (index === 4) {
      //     if (data.length < 1) return
      //     const values = data.map(item => Number(item[column.property]))
      //     const sum = values.reduce((prev, curr) => {
      //       return prev + curr
      //     })
      //     sums[index] = '本次绑定数量之和:' + sum
      //     this.sumNumber = sum
      //     return
      //   }
      // })
      // return sums
    },
    // 绑定数量校验
    currentAmountInput(value, row, name) {
      const data = row.row
      const val = inputValidation(value, data)
      data[name] = val ? val[0] - 0 : 0
    },
    // 保存
    handleSend() {
      // const change = contrastArray(this.beforeTableData, this.tableData)
      // if (change.length === 0) {
      //   this.$message.warning('没有任何修改，请关闭页面或修改本次绑定数量后再保存！')
      //   return
      // }
      // console.log(this.sumNumber, this.form.amount)
      // if (this.form.amount - 0 !== this.sumNumber) {
      //   this.$message.warning('本次可绑定数量之和必须与可绑定总数相等！')
      //   return
      // }
      // const title = '点击保存后，在下次退审前将不允许修改建设子专业绑定信息,请确认是否需要保存！'
      // this.$confirm(title, '系统提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //     cancelButtonClass: 'pms-zr-button',
      //     confirmButtonClass: 'pms-zr-button-primary',
      //   type: 'warning'
      // }).then(async(res) => {
      //   const obj = {
      //     bundingId: this.form.bundingId,
      //     productSubSpecInfo: this.tableData
      //   }
      //   const response = await Axios.saveSubSpecBundingDetail(obj)
      //   if (response.code !== '200') {
      //     return
      //   }
      //   this.$message.success(response.message)
      //   this.open = false
      //   this.$emit('getList')
      // })
    }
  }
}
</script>

<style scoped>
::v-deep .inputTable .el-input__inner {
  text-align: center;
  height: 20px;
}

::v-deep .inputTable .el-input {
  height: 20px;
}

::v-deep .dialog-footer {
  display: flex;
  justify-content: center;
}
</style>

